I've got a script that will return the parent of an object, but am struggling with how to call a grandparent or great-grandparent. This is for a separate layout column from Main that will show Object Number and Object Heading if Object Heading exists. I do need the data in a separate column so simply filtering with ancestors is not an option. |
Re: Code for getting the grandparent of an object? Thanks |
Re: Code for getting the grandparent of an object? SystemAdmin - Fri Mar 15 15:14:01 EDT 2013 This may be unhelpful, but I think you should try to solve this yourself using a recursive function. What you're asking for isn't that hard, but if you've never done it before and you figure out how to do it you will learn a lot. If someone just gives you the answer you won't learn anything. I'll give you a hint: the function you call will take two parameters: an Object and an int. The int represents the number of levels up to go. Good luck. |
Re: Code for getting the grandparent of an object? kbmurphy - Fri Mar 15 15:59:04 EDT 2013 |
Re: Code for getting the grandparent of an object? kbmurphy - Fri Mar 15 15:59:04 EDT 2013 //check object's level(?), then check object's parent(level minus 1) for a heading //if heading, display Object Number & Heading of object's parent found, if heading empty go to next level up and check for heading(1...n) you're right tho, I did say it backwards. will sleep on it and see if fresh eyes can get it tomorrow since we're working the weekend on this. thanks |
Re: Code for getting the grandparent of an object? SystemAdmin - Fri Mar 15 16:17:24 EDT 2013 Do a for loop where the beginning of the loop starts at 7, and the condition for the end of the loop is i==3, and at the end of the loop decrement by 1. In the loop get the parent of the object. Simple, yet effective. You just have to substitute 7 for the current level and 3 for the desired level. Get some sleep and refresh your brain. Good luck. |
Re: Code for getting the grandparent of an object? kbmurphy - Fri Mar 15 17:33:37 EDT 2013 |
Re: Code for getting the grandparent of an object? kbmurphy - Fri Mar 15 17:35:03 EDT 2013 so here's what I've got so far, it's still only doing parents one level up, and not grandparents. Another hint?
//STILL VERY MUCH IN WORK
//STILL ONLY DOING PARENTS, NOT GRANDPARENTS
string s = null
string p = null
//set up object and object's parent
Object o = obj
string oh = o."Object Heading"
Object po = parent o
//set up the count for level of object 10 is the lowest
//we'll start at 10 then check up the tree
int i = 10
while (i > 0){
//check parent object heading
if (oh == null){ //check if current object's heading is empty, don't want to do headers
p = po."Object Heading"
//check if parent object heading is not empty
if (p != null) {
s = probeRichAttr_(po,"Object Number", false)
q = probeRichAttr_(po,"Object Heading", false)
displayRich s " " q
break
} //endof parent not empty
//if parent object heading empty go to next level
if (p == null) {
i-- //decrement integer
break
} //endof parent empty
} //endof current object heading empty
break //break out of while loop when heading found
} //endof while(true)
|
Re: Code for getting the grandparent of an object?
Do you mean? ... Object o = obj Object po = parent o Object gpo = parent po |
Re: Code for getting the grandparent of an object? SystemAdmin - Sat Mar 16 09:17:37 EDT 2013
Do you mean? ... Object o = obj Object po = parent o Object gpo = parent po |
Re: Code for getting the grandparent of an object? SystemAdmin - Sat Mar 16 10:29:17 EDT 2013
Object objMasterParent = obj;
while (!null(parent(objMasterParent))) { objMasterParent = parent(objMasterParent) }
|
Re: Code for getting the grandparent of an object? SystemAdmin - Sat Mar 16 10:44:07 EDT 2013
Object objMasterParent = obj;
while (!null(parent(objMasterParent))) { objMasterParent = parent(objMasterParent) }
|
Re: Code for getting the grandparent of an object? SystemAdmin - Sat Mar 16 11:14:34 EDT 2013
I think Wolfgang's example was far too complex, and it doesn't do what you need it to do.
Object returnParentObjectAtLevel(int iLevel, Object oGiven) {
Object toReturn = null
//get level of passed object
int iObjLevel = level oGiven
//error checking: if iLevel >= iObjLevel, we should return null and optionally provide an error
if (iLevel>=iObjLevel) {
//print "ERROR: iLevel cannot be greater than the level of the passed object!"
return null
}
//do the work
toReturn = oGiven
while (iObjLevel >= iLevel) {
toReturn = parent(toReturn)
//set new value for iObjLevel
iObjLevel = level toReturn
//put your if statement here that says if Object Heading != null then break...whatever logic you want
}
return toReturn
}
Object oTemp = returnParentObjectAtLevel(3, obj)
if (oTemp != null) {
display number(oTemp) " " oTemp."Object Heading" ""
}
|
Re: Code for getting the grandparent of an object? I'm thinking that when you say "parent" you mean the "Object Heading" under which this object is seen when the module is viewed in Standard view. Thus the "parent" of object "1 Scope" is "Scope". And I think "grand parent" is the "Object Heading" above this object's "parent" heading.
If so,
Object GetParent(Object in_obj)
{ if (null in_obj) return(Object null)
string Head = probeAttr_(in_obj, "Object Heading")
if (!null Head) //
then return(in_obj)
else return(GetParent(parent(in_obj)) // ** RECURSION **
}
Object oCurr = current
Object oParent = GetParent(oCurr)
Object oGrandParent = GetParent(parent(oParent)) // check for null oParent first
|
Re: Code for getting the grandparent of an object? SystemAdmin - Sat Mar 16 11:14:34 EDT 2013
Object objMasterParent = obj;
while (!null(parent(objMasterParent))) {
int iLevel = level(objMasterParent);
int iAbsNum = objMasterParent."Absolute Number";
string sObjectText = objMasterParent."Object Text";
string sObjectHeading = objMasterParent."Object Heading";
string sMessage = "";
if (sObjectText = "") {
sMessage = "Text-Object " iAbsNum " found on level " iLevel " Text:" sObjectText;
} else {
sMessage = "Heading-Object " iAbsNum " found on level " iLevel " Heading:" sObjectText;
}
print sMessage "\n";
objMasterParent = parent(objMasterParent)
}
|
Re: Code for getting the grandparent of an object? SystemAdmin - Wed Mar 20 15:25:29 EDT 2013
Object objMasterParent = obj;
while (!null(parent(objMasterParent))) {
int iLevel = level(objMasterParent);
int iAbsNum = objMasterParent."Absolute Number";
string sObjectText = objMasterParent."Object Text";
string sObjectHeading = objMasterParent."Object Heading";
string sMessage = "";
if (sObjectText = "") {
sMessage = "Text-Object " iAbsNum " found on level " iLevel " Text:" sObjectText;
} else {
sMessage = "Heading-Object " iAbsNum " found on level " iLevel " Heading:" sObjectText;
}
print sMessage "\n";
objMasterParent = parent(objMasterParent)
}
I've got the following doing what I want to do: show the parent object number and heading of the current object. It only shows the first parent then kicks out, so it does not show all the parents up the chain like the ancestors function would in a filter.
//This script will give the parent number and heading of the current object in a layout
//dxl column
string s = null
string p = null
//master object for loop
Object objMasterParent = obj
//while master object is not empty
while (!null(parent(objMasterParent))) {
int iLevel = level(objMasterParent)
int iAbsNum = objMasterParent."Absolute Number"
string sObjectText = objMasterParent."Object Text"
string sObjectHeading = objMasterParent."Object Heading"
string sMessage = ""
//if object text of master object is empty
if (sObjectText == "") {
s = probeRichAttr_(objMasterParent,"Object Number", false)
p = sObjectHeading
displayRich "Section: " s " " p
//we only want the one level up heading(parent) of the object, not all the way like ancestors in a filter so kick out if we've got it
break
} else {
}
objMasterParent = parent(objMasterParent)
}
string sObjectNumber = objMasterParent."Object Number"
|
Re: Code for getting the grandparent of an object? SystemAdmin - Thu Mar 21 08:26:15 EDT 2013
I've got the following doing what I want to do: show the parent object number and heading of the current object. It only shows the first parent then kicks out, so it does not show all the parents up the chain like the ancestors function would in a filter.
//This script will give the parent number and heading of the current object in a layout
//dxl column
string s = null
string p = null
//master object for loop
Object objMasterParent = obj
//while master object is not empty
while (!null(parent(objMasterParent))) {
int iLevel = level(objMasterParent)
int iAbsNum = objMasterParent."Absolute Number"
string sObjectText = objMasterParent."Object Text"
string sObjectHeading = objMasterParent."Object Heading"
string sMessage = ""
//if object text of master object is empty
if (sObjectText == "") {
s = probeRichAttr_(objMasterParent,"Object Number", false)
p = sObjectHeading
displayRich "Section: " s " " p
//we only want the one level up heading(parent) of the object, not all the way like ancestors in a filter so kick out if we've got it
break
} else {
}
objMasterParent = parent(objMasterParent)
}
string sObjectNumber = objMasterParent."Object Number"
|
Re: Code for getting the grandparent of an object? SystemAdmin - Thu Mar 21 09:43:31 EDT 2013
|